*recoding ZA4662 (2005 observations only)

use "Data\ZA4662\ZA4662_v1-1-0.dta" , clear

keep if d==1 //drop all observations apart of those from 2005; 2002 and 1998 were recoded seperately

*Support for democracy
 *not included
 
*Support for liberal democracy
gen $libdem_opposition			= d170k
gen $libdem_freeopinion			= d170h

*Vertrauen Institutionen
gen $trust_parties = d220h
gen $trust_government = d220c
gen $trust_verfassungsgericht = d220b




*redefine missing values
replace $libdem_opposition 	= .a if $libdem_opposition 	== .v 	//.a = no answer
replace $libdem_opposition  = .b if $libdem_opposition	== .w  	//.b = don't know 
replace $libdem_opposition  = .c if $libdem_opposition	== .  	//.c = undefined

replace $libdem_freeopinion = .a if $libdem_freeopinion 	== .v 	//.a = no answer
replace $libdem_freeopinion = .b if $libdem_freeopinion	== .w  	//.b = don't know 
replace $libdem_freeopinion  = .c if $libdem_freeopinion	== .  	//.c = undefined
*Standardize range from 0 to 1; higher values indicate higher support for democracy

foreach var of varlist $libdem_opposition $libdem_freeopinion {
	replace `var'	= (`var'-1)/4 if !missing(`var')
}


foreach var of varlist $trust_parties $trust_government $trust_verfassungsgericht {
	replace `var' = (`var' - 1)/4 if !missing(`var')
}


*Sociodemographic Controls
gen			$gender 			= dsex-1
replace 	$gender				= .a if $gender == .	// .a = no answer

gen 		$age  				= dalter
gen 		$education			= dbildga

*redefine missing values 
replace 	$education			= .b if $education == .w  						//.b = don't know 
replace 	$education			= .c if $education == .  						//.c = undefined
replace 	$education 			= 0  if $education <= 4 & !missing($education)  // still at school, none, Hautpschule, Mittlere Reife = 0
replace 	$education 			= 1  if $education >= 5	& !missing($education)  // Abitur, EOS, university degree = 1

gen 		$eastwest 			= dsplitwo-1


*Party ID
*Did R lean towards  a left, right or other party?
*1: right, (CDU/CSU, FDP, AfD), 0: left (Left/PDS, Greens, SPD), -1: other
gen $partyid_category		= 1 	if inlist(dpid, 1, 2, 4)
replace $partyid_category	= 0 	if inlist(dpid, 3, 5, 6)
replace $partyid_category	= -1 	if inlist(dpid, 8, 9, 10)


*Party Vote
*Did R lean towards  a left, right or other party?
*1: right, (CDU/CSU, FDP, AfD), 0: left (Left/PDS, Greens, SPD), -1: other
gen $partyvote_category		= 1 	if inlist(d70, 1, 3)
replace $partyvote_category	= 0 	if inlist(d70, 2, 4, 5)
replace $partyvote_category	= -1 	if inlist(d70, 7, 8)




*Meta variables
gen $dataset 					= "ZA4662"
gen $year 						= 2005

*Weight
gen weight = dggesow

*check
tab $libdem_opposition d170k, m
tab $libdem_freeopinion d170h, m
tab $gender dsex, m
tab $age, m
tab $education dbildga, m
tab $eastwest dsplitwo, m	

*Save recode dataset
keep $libdem_opposition	$libdem_freeopinion $trust_parties $trust_government $trust_verfassungsgericht $gender $age $education $eastwest  	///
	$dataset $year  weight	$partyid_category	$partyvote_category	
save ZA4662_recoded.dta, replace
